<template>
  <div class="detail_box">
    <!-- 操作 -->
    <div class="operate_box">
      <div class="item_box">
        <div class="operate_item">
          <label>关键词：</label>
          <el-input type="text" placeholder="请输入关键词" v-model="keyword"></el-input>
        </div>
        <div class="operate_item">
          <label>指令：</label>
          <el-select
            filterable
            v-model="instruct"
            placeholder="请选择指令">
            <el-option
              v-for="(item,index) in instructOptions"
              :key="index"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </div>
        <div class="operate_item">
          <span class="operate_btn search_btn" @click="getTableInfo(1)">查询</span>
          <span class="operate_btn reset_btn" @click="resetTable">重置</span>
          <span class="operate_btn export_btn" @click="checkSelect">批量处理</span>
        </div>
      </div>

    </div>
    <!-- 表格 -->
    <div class="table_box">
      <el-table
        border
        height="480"
        v-loading="loading"
        :header-cell-style="tableHeader"
        ref="multipleTable"
        :data="tableInfo"
        tooltip-effect="dark"
        :row-key="getRowKeys"
        @selection-change="selectionChange"
        style="width: 100%;">
        <el-table-column
          type="selection"
          :reserve-selection="true"
          width="55">
        </el-table-column>
        <el-table-column
          width="100"
          prop="id"
          label="ID">
        </el-table-column>
        <el-table-column
          width="100"
          prop="deviceId"
          label="控制器编号">
        </el-table-column>
        <el-table-column
          sortable
          width="160"
          prop="modifyTime"
          label="修改时间">
          <template slot-scope="scope">
            <p style="text-align: left">{{ formatTime(scope.row.modifyTime) }}</p>
          </template>
        </el-table-column>
        <el-table-column
          width="100"
          prop="terminalCode"
          label="充电终端">
        </el-table-column>
        <el-table-column
          width="160"
          prop="typeName"
          label="告警类型">
        </el-table-column>
        <el-table-column
          width="160"
          label="回调未解析数据">
          <template slot-scope="scope">
            <p>{{ scope.row.sourceData }}</p>
          </template>
        </el-table-column>
        <el-table-column
          label="指令">
          <template slot-scope="scope">
            <p>{{ scope.row.instructName }}(<span style="color:#5ca4fa;">{{ scope.row.instruct }}</span>)</p>
          </template>
        </el-table-column>
        <el-table-column
          prop="dataInfo"
          label="数据解析">
        </el-table-column>
        <el-table-column
          width="80"
          label="状态">
          <template slot-scope="scope">
            <p class="normal" :class="{'check':scope.row.enable===2}">{{ scope.row.enable === 1 ? '已查看' : '未查看' }}</p>
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          width="140"
          label="操作">
          <template slot-scope="scope">
            <div class="table_btn">
              <span class="change_info" @click="updateData(scope.row.id)"
                    v-if="scope.row.instruct=='S99'&&scope.row.enable===2">更新</span>
              <span class="change_info" @click="resetWarning(scope.row.id)">重新预警</span>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="record_page">
      <span class="page_count">共<i>{{ totals }}</i>条</span>
      <el-pagination
        @size-change="sizeChange"
        @current-change="getTableInfo"
        background
        :current-page="currentPage"
        :page-sizes="[15, 20, 30, 40]"
        :page-size="pageSize"
        layout="prev, pager, next, sizes,jumper"
        :total="totals">
      </el-pagination>
    </div>
  </div>
</template>

<script>

import commons from "@/utils/common";
import {getChargeRecord, reloadCharge, updateChargeData, updateChargeEnable} from "../../../api/device/record";

export default {
  props: ['deviceId'],
  data() {
    return {
      keyword: '',
      instructOptions: [{id: '', name: '全部'}, {id: 'S99', name: 'S99'}, {id: 'S01', name: 'S01'}],
      instruct: '',
      currentPage: 1,
      pageSize: 15,
      totals: 0,
      loading: false,
      tableInfo: [],
      firstMenu: '',
      selectItem: [],
    }
  },
  mounted() {
    this.getTableInfo(1);
  },
  methods: {
    // 获取列表信息
    getTableInfo(page) {
      let that = this;
      that.loading = true;
      that.currentPage = page;
      let queryParams = {
        limit: that.pageSize,
        page: current,
        keyword: that.keyword,
        instruct: that.instruct,
        deviceId: that.deviceId
      }
      getChargeRecord(queryParams).then(res => {
        that.tableInfo = res.data ? res.data.list : [];
        that.totals = res.data ? res.data.count : 0;
        that.loading = false;
      }, (err) => {
        that.loading = false;
      });
    },

    // 重置
    resetTable() {
      this.pageSize = 15;
      this.keyword = '';
      this.instruct = '';
      this.getTableInfo(1)
    },

    // 获取选中列的id
    getRowKeys(row) {
      return row.id;
    },

    // 多选
    selectionChange(val) {
      let that = this;
      let data = '';
      val.forEach((item, i) => {
        if (item.enable === 2) {
          data = data + (i > 0 ? ',' : '') + item.id;
        }
      });
      that.selectItem = data;
    },

    // 批量处理
    checkSelect() {
      let that = this;
      if (that.selectItem && that.selectItem.length > 0) {
        that.checkItem()
      } else {
        that.$message.error('当前没有设备需要进行处理');
      }
    },

    checkItem() {
      let that = this;
      this.$confirm('确定要批量查看所选设备?', '提示', {
        type: 'warning'
      }).then(() => {
        that.loading = true;
        updateChargeEnable({ids: that.selectItem}).then(res => {
          if (res.success) {
            that.$message.success('操作成功！');
            that.getTableInfo(that.currentPage);
          }
          that.loading = false;
        }, (err) => {
          that.loading = false;
        });
      }).catch(() => {
      });
    },

    // 更新数据
    updateData(id) {
      let that = this;
      that.loading = true;
      updateChargeData({id: id}).then(res => {
        if (res.success) {
          that.$message.success('操作成功！');
          that.getTableInfo(that.currentPage);
        }
        that.loading = false;
      }, (err) => {
        that.loading = false;
      });
    },

    // 重新预警
    resetWarning(id) {
      let that = this;
      that.loading = true;
      reloadCharge({id: id}).then(res => {
        if (res.data.success) {
          that.$message.success('设置成功');
        } else {
          that.$message.error(res.data.message);
        }
        that.loading = false;
      }, (err) => {
        that.loading = false;
      });
    },

    comeback() {
      this.$router.go(-1);
    },

    formatTime(time) {
      return commons.formatTime(time)
    },

    sizeChange(val) {
      this.pageSize = val;
      this.getTableInfo(1)
    },

    // 设置table标题居中
    tableHeader({row, column, rowIndex, columnIndex}) {
      return 'background:#F0F2F5;color:#111;padding:10px 0;font-size:14px;font-weight:600'
    },
  }
}
</script>


<style>
.el-dialog__body {
  padding: 0 20px 20px !important;
}
</style>
<style scoped>
.detail_box {
  position: relative;
  width: 100%;
  box-sizing: border-box;
  min-width: 0;
  height: 600px;
  min-height: 600px;
  padding: 0;
}

.check {
  color: #f7ab20;
}

.export_btn {
  background: #f7ab20;
  color: #fff;
  margin-left: 15px;
}
</style>
